import { reactive } from "vue";

// 参数控制
// 【reactive一定要加，会实时传输数据的】
export const ADS = reactive({
  ADS_SCALE1_REALWGHT: 0,
  ADS_D22203_L: 0,
  TankNo: 0,
  IngreName: "null",
  IngreCode: "null",
  ads_plc_status: 0,
})

export const tds = reactive({
  tds_plc_status: 0,
  tds_premix_temperature: 0, //预混温度设定
  tds_motor_temp: 0,         //电机温度
  tds_actual_current: 0,
  tds_actual_power: 0,
  tds_speed_set: 0,          //速度设定
  tds_time_set: 0,           //时间设定
  tds_mixspeed_set: 0,
  tds_target_set: 0,
  tds_dischargespeed_set: 0,
  tds_draintime_set: 0,
})

export const T3 = reactive({
  T3_PwsNo: "null",
  T3_MaterialName: "null",
  T3_BatchNo: "null",
  T3_PlannedQty: "null",
  T3_WEGT: 0,
  T3_FrequencySetValue: 0,
  T3_FrequencyActualValue: 0,
  T3_RSpeedActualValue: 0,
  T3_RSpeedSetValue: 0,
  T3_FrequencyConverter_Running: false,
  T3_CurrentActualValue: 0,
  T3_AccRunTime: 0,
  T3_ConverterErrorCode: 0,
  T3_ConverterFaultCode: 0,
  // 补充生产信息字段
  T3_timestamp: "null",
})

export const T4 = reactive({
  T4_PwsNo: "null",
  T4_MaterialName: "null",
  T4_BatchNo: "null",
  T4_PlannedQty: "null",
  T4_WEGT: 0,
  T4_FrequencySetValue: 0,
  T4_FrequencyActualValue: 0,
  T4_RSpeedActualValue: 0,
  T4_RSpeedSetValue: 0,
  T4_FrequencyConverter_Running: false,
  T4_CurrentActualValue: 0,
  T4_AccRunTime: 0,
  T4_ConverterErrorCode: 0,
  T4_ConverterFaultCode: 0,
  T4_timestamp: "null",
})

export const T7K = reactive({
  T7K_PwsNo: "null",
  T7K_MaterialName: "null",
  T7K_BatchNo: "null",
  T7K_PlannedQty: "null",
  T7K_WEGT: 0,
  T7K_FrequencySetValue: 0,
  T7K_FrequencyActualValue: 0,
  T7K_RSpeedActualValue: 0,
  T7K_RSpeedSetValue: 0,
  T7K_FrequencyConverter_Running: false,
  T7K_CurrentActualValue: 0,
  T7K_AccRunTime: 0,
  T7K_ConverterErrorCode: 0,
  T7K_ConverterFaultCode: 0,
  T7K_timestamp: "null",
})

export const T10K = reactive({
  T10K_PwsNo: "null",
  T10K_MaterialName: "null",
  T10K_BatchNo: "null",
  T10K_PlannedQty: "null",
  T10K_WEGT: 0,
  T10K_FrequencySetValue: 0,
  T10K_FrequencyActualValue: 0,
  T10K_RSpeedActualValue: 0,
  T10K_RSpeedSetValue: 0,
  T10K_FrequencyConverter_Running: false,
  T10K_CurrentActualValue: 0,
  T10K_AccRunTime: 0,
  T10K_ConverterErrorCode: 0,
  T10K_ConverterFaultCode: 0,
  T10K_timestamp: "null",
})

export const mes = {
  mes_plc_status: 0,
}

export const scale = {
  scale_27: 0,
  scale_28: 0,
}

export const RUN = {
  ads01run: false,
  ads02run: false,
  ads03run: false,
  ads04run: false,
  ads05run: false,
  ads06run: false,
  ads07run: false,
  ads08run: false,
  ads09run: false,
  ads10run: false,
  ads11run: false,
  ads12run: false,
  ads13run: false,
  ads14run: false,
  ads15run: false,
  ads16run: false,
  ads17run: false,
  ads18run: false,
  ads19run: false,
  ads20run: false,
  ads21run: false,
};

// 创建 WebSocket
const socket = new WebSocket('ws://132.0.140.25:10001');

// WebSocket连接已建立
socket.onopen = () => {
  console.log('WebSocket连接已建立');
};

// WebSocket接收数据
socket.onmessage = (event) => {
  try {
    const data = JSON.parse(event.data); // 解析 JSON
    // console.log("收到的 JSON 数据：", data);

    // 判断是否为数组
    if (Array.isArray(data)) {
      ProcessDataArray(data); // 如果是数组，调用 ProcessDataArray
    } else {
      ProcessDataNew(data); // 否则调用 ProcessDataNew
    }
  } catch (error) {
    console.error("JSON 解析错误:", error);
  }
};

socket.onclose = () => {
  console.log("WebSocket连接已关闭");
};

socket.onerror = (error) => {
  console.error("WebSocket错误:", error);
};

function ProcessDataArray(datas) {
  console.log('数组', datas);
  datas.forEach(element => {
    ProcessDataNew(element);
  });
}

function ProcessDataNew(data) {
  // T3相关数据更新
  if (data.name === 'T3_AccRunTime') {
    T3.T3_AccRunTime = data.value.toFixed(3);
  }
  if (data.name === 'T3_ConverterErrorCode') {
    T3.T3_ConverterErrorCode = data.value.toFixed(3);
  }
  if (data.name === 'T3_ConverterFaultCode') {
    T3.T3_ConverterFaultCode = data.value.toFixed(3);
  }
  if (data.name === 'T3_CurrentActualValue') {
    T3.T3_CurrentActualValue = data.value.toFixed(3);
  }
  if (data.name === 'T3_FrequencyConverter_Running') {
    T3.T3_FrequencyConverter_Running = data.value;
  }
  if (data.name === 'T3_RSpeedSetValue') {
    T3.T3_RSpeedSetValue = data.value.toFixed(3);
  }
  if (data.name === 'T3_RSpeedActualValue') {
    T3.T3_RSpeedActualValue = data.value.toFixed(3);
  }
  // 新增 T3生产单信息
  if (data.name === 'T3_PwsNo') {
    // 如果需要转字符，可以根据数据格式自行调整
    T3.T3_PwsNo = String.fromCharCode(...data.value.data);
    T3.T3_timestamp = data.timestamp || T3.T3_timestamp;
  }
  if (data.name === 'T3_MaterialName') {
    T3.T3_MaterialName = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T3_BatchNo') {
    T3.T3_BatchNo = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T3_PlannedQty') {
    T3.T3_PlannedQty = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T3_WEGT') {
    T3.T3_WEGT = data.value.toFixed(3);
  }
  if (data.name === 'T3_FrequencyActualValue') {
    T3.T3_FrequencyActualValue = data.value.toFixed(3);
  }

  // T4相关数据更新
  if (data.name === 'T4_AccRunTime') {
    T4.T4_AccRunTime = data.value.toFixed(3);
  }
  if (data.name === 'T4_ConverterErrorCode') {
    T4.T4_ConverterErrorCode = data.value.toFixed(3);
  }
  if (data.name === 'T4_ConverterFaultCode') {
    T4.T4_ConverterFaultCode = data.value.toFixed(3);
  }
  if (data.name === 'T4_CurrentActualValue') {
    T4.T4_CurrentActualValue = data.value.toFixed(3);
  }
  if (data.name === 'T4_FrequencyConverter_Running') {
    T4.T4_FrequencyConverter_Running = data.value;
  }
  if (data.name === 'T4_RSpeedSetValue') {
    T4.T4_RSpeedSetValue = data.value.toFixed(3);
  }
  if (data.name === 'T4_RSpeedActualValue') {
    T4.T4_RSpeedActualValue = data.value.toFixed(3);
  }
  // 新增 T4生产单信息
  if (data.name === 'T4_PwsNo') {
    T4.T4_PwsNo = String.fromCharCode(...data.value.data);
    T4.T4_timestamp = data.timestamp || T4.T4_timestamp;
  }
  if (data.name === 'T4_MaterialName') {
    T4.T4_MaterialName = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T4_BatchNo') {
    T4.T4_BatchNo = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T4_PlannedQty') {
    T4.T4_PlannedQty = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T4_WEGT') {
    T4.T4_WEGT = data.value.toFixed(3);
  }
  if (data.name === 'T4_FrequencyActualValue') {
    T4.T4_FrequencyActualValue = data.value.toFixed(3);
  }

  // ADS相关数据更新
  if (data.name === 'ADS_DATAS') {
    if (data.IngreNo === 0) {
      ADS.IngreCode = "null";
      ADS.IngreName = "null";
      ADS.TankNo = 0;
    } else {
      const ingreNo = data.IngreNo;
      const adsKey = `ads${ingreNo.toString().padStart(2, '0')}run`;  // 生成属性名，比如 "ads01run" 或 "ads02run"
      // 设置 ADS 数据
      ADS.IngreCode = data.IngreCode;
      ADS.IngreName = data.IngreName;
      ADS.TankNo = data.TankNo;
      // 清空 RUN 中的所有 adsXrun 属性，并设置对应的 adsXXrun 为 true
      Object.keys(RUN).forEach(key => {
        RUN[key] = key === adsKey;
      });
    }
  }
  if (data.name === 'ADS_D7020_L') {
    ADS.ADS_D22203_L = data.value.toFixed(3);
  }
  if (data.name === 'ADS_SCALE1_REALWGHT') {
    ADS.ADS_SCALE1_REALWGHT = data.value.toFixed(3);
  }
  if (data.name === 'ads_plc_status') {
    // 修正变量名，确保更新的是 ADS 对象
    ADS.ads_plc_status = data.value;
  }

  // T7K相关数据更新
  if (data.name === 'T7K_AccRunTime') {
    T7K.T7K_AccRunTime = data.value.toFixed(3);
  }
  if (data.name === 'T7K_ConverterErrorCode') {
    T7K.T7K_ConverterErrorCode = data.value.toFixed(3);
  }
  if (data.name === 'T7K_ConverterFaultCode') {
    T7K.T7K_ConverterFaultCode = data.value.toFixed(3);
  }
  if (data.name === 'T7K_CurrentActualValue') {
    T7K.T7K_CurrentActualValue = data.value.toFixed(3);
  }
  if (data.name === 'T7K_FrequencyConverter_Running') {
    T7K.T7K_FrequencyConverter_Running = data.value;
  }

  // this
  if (data.name === 'T7K_WEGT') {
    T7K.T7K_WEGT = data.value;
    console.log(777777777777, T7K.T7K_WEGT);
  }
  // this

  if (data.name === 'T7K_RSpeedSetValue') {
    T7K.T7K_RSpeedSetValue = data.value.toFixed(3);
  }
  if (data.name === 'T7K_RSpeedActualValue') {
    T7K.T7K_RSpeedActualValue = data.value.toFixed(3);
  }
  if (data.name === 'T7K_PwsNo') {
    // 假设数据以字符数组形式传递，可按需要调整
    T7K.T7K_PwsNo = String.fromCharCode(...data.value.data);
    T7K.T7K_timestamp = data.timestamp;
  }
  if (data.name === 'T7K_MaterialName') {
    T7K.T7K_MaterialName = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T7K_PlannedQty') {
    T7K.T7K_PlannedQty = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T7K_BatchNo') {
    T7K.T7K_BatchNo = String.fromCharCode(...data.value.data);
  }

  // T10K相关数据更新
  if (data.name === 'T10K_AccRunTime') {
    T10K.T10K_AccRunTime = data.value.toFixed(3);
  }
  if (data.name === 'T10K_ConverterErrorCode') {
    T10K.T10K_ConverterErrorCode = data.value.toFixed(3);
  }
  if (data.name === 'T10K_ConverterFaultCode') {
    T10K.T10K_ConverterFaultCode = data.value.toFixed(3);
  }
  if (data.name === 'T10K_CurrentActualValue') {
    T10K.T10K_CurrentActualValue = data.value.toFixed(3);
  }
  if (data.name === 'T10K_FrequencyConverter_Running') {
    T10K.T10K_FrequencyConverter_Running = data.value;
  }
  if (data.name === 'T10K_RSpeedSetValue') {
    T10K.T10K_RSpeedSetValue = data.value.toFixed(3);
  }
  if (data.name === 'T10K_RSpeedActualValue') {
    T10K.T10K_RSpeedActualValue = data.value.toFixed(3);
  }
  // 新增 T10K生产单信息
  if (data.name === 'T10K_PwsNo') {
    T10K.T10K_PwsNo = String.fromCharCode(...data.value.data);
    T10K.T10K_timestamp = data.timestamp || T10K.T10K_timestamp;
  }
  if (data.name === 'T10K_MaterialName') {
    T10K.T10K_MaterialName = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T10K_BatchNo') {
    T10K.T10K_BatchNo = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T10K_PlannedQty') {
    T10K.T10K_PlannedQty = String.fromCharCode(...data.value.data);
  }
  if (data.name === 'T10K_WEGT') {
    T10K.T10K_WEGT = data.value.toFixed(3);
  }
  if (data.name === 'T10K_FrequencyActualValue') {
    T10K.T10K_FrequencyActualValue = data.value.toFixed(3);
  }

  // scale相关数据更新
  if (data.name === 'scale_28') {
    scale.scale_28 = data.value.toFixed(3);
  }
  if (data.name === 'scale_27') {
    scale.scale_27 = data.value.toFixed(3);
  }

  // tds相关数据更新
  if (data.name === 'tds_plc_status') {
    tds.tds_plc_status = data.value;
  }
  if (data.name === 'tds_actual_current') {
    tds.tds_actual_current = data.value.toFixed(3);
  }
  if (data.name === 'tds_dischargespeed_set') {
    tds.tds_dischargespeed_set = data.value.toFixed(3);
  }
  if (data.name === 'tds_draintime_set') {
    tds.tds_draintime_set = data.value.toFixed(3);
  }
  if (data.name === 'tds_mixspeed_set') {
    tds.tds_mixspeed_set = data.value.toFixed(3);
  }
  if (data.name === 'tds_motor_temp') {
    tds.tds_motor_temp = data.value.toFixed(3);
  }
  if (data.name === 'tds_premix_temperature') {
    tds.tds_premix_temperature = data.value.toFixed(3);
  }
  if (data.name === 'tds_speed_set') {
    tds.tds_speed_set = data.value.toFixed(3);
  }
  if (data.name === 'tds_target_set') {
    tds.tds_target_set = data.value.toFixed(3);
  }
  if (data.name === 'tds_actual_power') {
    tds.tds_actual_power = data.value.toFixed(3);
  }
}